package com.hyg.lib_music.ui.activity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.Base.BaseApplication;
import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_common.DataModel.Music.MusicCateData;
import com.hyg.lib_common.DataModel.Music.MusicCategoryData;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_common.ui.activity.BlueToothListActivity;
import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.TabPagerAdapter;

import com.hyg.lib_music.presenter.MainMusicBedContract;
import com.hyg.lib_music.presenter.di.components.ApplicationComponent;
import com.hyg.lib_music.presenter.di.components.DaggerActivityComponent;
import com.hyg.lib_music.service.MusicService;
import com.hyg.lib_music.ui.fragment.BaseFragment;
import com.hyg.lib_music.ui.fragment.MusicCateListFragment;
import com.hyg.lib_music.ui.fragment.MusicCategoryListFragment;
import com.hyg.lib_music.ui.fragment.MusicDocListFragment;
import com.hyg.lib_music.ui.fragment.MusicFavListFragment;
import com.hyg.lib_music.ui.fragment.MusicListFragment;
import com.hyg.lib_music.ui.fragment.MusicRecommendFragment;
import com.hyg.lib_music.ui.view.MusicPlayView;
import com.hyg.lib_music.ui.view.MusicTabView;
import com.hyg.lib_music.util.ApplicationComponentUtil;
import com.hyg.lib_music.util.MusicChangeEvent;
import com.hyg.lib_music.util.MusicFavManager;
import com.hyg.lib_music.util.SrceenUtils;
import com.hyg.lib_music.util.TimeUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.litepal.LitePal;
import org.litepal.tablemanager.callback.DatabaseListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.inject.Inject;


//@Route(path = Constants.PATH_ACTIVITY_MODULE_HEALTH_MUSICBED)
public class MainMusicBedActivity extends BaseActivity
//        implements View.OnClickListener
{

    private ImageView back;
    private ImageView musicClock;

    public MusicPlayView musicPlayView;//播放器
    private List<BaseFragment> fragmentList;//容器集合
    private MusicService.MusicServiceBinder serviceBinder;

//    @Inject
//    MainMusicBedContract.Presenter presenter;
//
//    private int stopSecond = 0;//倒计时
//    private Timer mTimer = null;
//    private OptionsPickerView pvOptions;

    //没有导航的
    private MusicCategoryData mCategory;
    private MusicCateData mCate;
    private ViewPager viewPager;
    private TabPagerAdapter mAdapter;

    //有导航的
    private MusicTabView tabWidget;//顶部导航
    //    private MyVolumeReceiver mVolumeReceiver;
//
//    public static ApplicationComponent mApplicationComponent;
    private MusicCategoryListFragment musicCategoryListFragment;
    MusicFavListFragment musicFavListFragment;
    MusicRecommendFragment musicRecommendFragment;


//    private BLEUtils mBleUtils;//蓝牙工具类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("MainMusicBedActivity", "onCreate start(): ");
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 60, null, null, true);
        init();

//        Log.i("MainMusicBedActivity", "onCreate end(): ");
//        startBatterylevelTimer();
//        registerReceiver();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        stopTimer();
//        stopBatterylevelTimer();
//        unbindService(mServiceConnection);
//        unregisterVolumeChangedReceiver();
//        unregisterReceiver();
    }

//    // 控件监听
//    @Override
//    public void onClick(View v) {
//        int id = v.getId();
//        if (id == R.id.play_pre) {// 上一首
//            serviceBinder.playPre();
//        } else if (id == R.id.play_next) {// 下一首
//            serviceBinder.playNext();
//        } else if (id == R.id.play_or_pause) {// 播放或暂停
//            serviceBinder.playOrPause();
//            //            case R.id.playing_list:
//            // 播放列表
////                showPlayList();
////                break;
//        } else if (id == R.id.back) {
//            finish();
//            overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
//        } else if (id == R.id.music_clock) {
////            showTimerDialog();
//            showBluetoothList();
//        } else if (id == R.id.music_type_order) {
//            serviceBinder.setPlayMode(MusicService.TYPE_ORDER);
//            musicPlayView.updateMusicType(MusicService.TYPE_ORDER);
////                Toast.makeText(MainMusicBedActivity.this, "列表循环", Toast.LENGTH_SHORT).show();
//        } else if (id == R.id.music_type_single) {
//            serviceBinder.setPlayMode(MusicService.TYPE_SINGLE);
//            musicPlayView.updateMusicType(MusicService.TYPE_SINGLE);
////                Toast.makeText(MainMusicBedActivity.this, "单曲循环", Toast.LENGTH_SHORT).show();
//        } else if (id == R.id.music_type_random) {
//            serviceBinder.setPlayMode(MusicService.TYPE_RANDOM);
//            musicPlayView.updateMusicType(MusicService.TYPE_RANDOM);
////                Toast.makeText(MainMusicBedActivity.this, "随机播放", Toast.LENGTH_SHORT).show();
//        } else if (id == R.id.fav) {//获得当前音乐
//            Music item = serviceBinder.getCurrentMusic();
//
//            //得到收藏状态
//            if (item != null) {
//                if (MusicFavManager.sharedManager().isExist(item.id)) {
//                    presenter.removeFavorite(item.id);
//                } else {
//                    presenter.addFavorite(item.id);
//                }
//            }
//        } else if (id == R.id.play_more) {
//            showMoreWindow(this);
//        } else if (id == R.id.close) {
//            showMoreWindow(this);
//        }
//    }

    private void init() {
        musicCategoryListFragment = new MusicCategoryListFragment();
        musicRecommendFragment = new MusicRecommendFragment();
        musicFavListFragment = new MusicFavListFragment();
        if (mCategory == null && mCate == null) {
            //首页有导航
            setContentView(R.layout.activity_main_music_bed);
            tabWidget = this.findViewById(R.id.tabWidget);

            //构造Fragment的集合
            fragmentList = new ArrayList<>();
            fragmentList.add(musicFavListFragment);
            fragmentList.add(musicCategoryListFragment);
            fragmentList.add(musicFavListFragment);
            //初始化TabWidget
            tabWidget.init(getSupportFragmentManager(), fragmentList, new MusicTabView.onMyTabChangeListener() {
                @Override
                public void onTabChanged(int position) {
//                    showMusicPlayView(position);
                }
            });
        }
        else {
            //子页面无导航
            setContentView(R.layout.activity_music_list);
            viewPager = this.findViewById(R.id.tab_viewPager);
            //构造Fragment的集合
            fragmentList = new ArrayList<>();
            if (mCategory != null) {

                if (mCategory.subList == null || mCategory.subList.size() == 0) {
                    fragmentList.add(MusicListFragment.newInstance(mCategory));
                } else {
                    fragmentList.add(musicCategoryListFragment);
                }
            } else if (mCate != null) {

                if (mCate.subList == null || mCate.subList.size() == 0) {
                    fragmentList.add(MusicDocListFragment.newInstance(mCate));
                } else {
                    fragmentList.add(MusicCateListFragment.newInstance(mCate));
                }
            }
//
            //初始化 ViewPager
            mAdapter = new TabPagerAdapter(getSupportFragmentManager(), fragmentList);
            viewPager.setAdapter(mAdapter);
        }
        musicPlayView = this.findViewById(R.id.musicPlayView);
        back = (ImageView) findViewById(R.id.back);
//        musicClock = (ImageView) findViewById(R.id.music_clock);
//        music_bluetooth_batterylevel_area =  findViewById(R.id.music_bluetooth_batterylevel_area);
//        music_bluetooth_batterylevel_emp = (ImageView)findViewById(R.id.music_bluetooth_batterylevel_emp);
//        music_bluetooth_batterylevel = (ImageView)findViewById(R.id.music_bluetooth_batterylevel);
//        music_bluetooth_batterylevel_text = (TextView) findViewById(R.id.music_bluetooth_batterylevel_text);
//
//        // 设置监听
//        back.setOnClickListener(this);
//        musicClock.setOnClickListener(this);
//        musicPlayView.btnPlayMode.setOnClickListener(this);
//        musicPlayView.btnPlayOrPause.setOnClickListener(this);
//        musicPlayView.btnPlayPre.setOnClickListener(this);
//        musicPlayView.btnPlayNext.setOnClickListener(this);
//        musicPlayView.btnPlayingList.setOnClickListener(this);
        //先隐藏进度
//        musicPlayView.seekBar.setVisibility(View.INVISIBLE);
//        musicPlayView.indicatorView.setVisibility(View.INVISIBLE);
//        musicPlayView.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                musicPlayView.indicatorView.setText(TimeUtils.formatTime((long) progress) + "/" + TimeUtils.formatTime((long) seekBar.getMax()));
//
//                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) musicPlayView.indicatorView.getLayoutParams();
//                float screenWidth = SrceenUtils.getScreenWidth(MainMusicBedActivity.this);
//                float indicatorViewWidth = musicPlayView.indicatorView.getWidth();
//                float indicatorOffset = screenWidth * ((float) progress / seekBar.getMax()) - indicatorViewWidth / 2;
//                if (indicatorOffset > 0 && indicatorOffset < screenWidth - (indicatorViewWidth)) {
//                    params.leftMargin = (int) indicatorOffset;
//                } else if (indicatorOffset > screenWidth - indicatorViewWidth) {
//                    params.leftMargin = (int) (screenWidth - indicatorViewWidth);
//                } else {
//                    params.leftMargin = 0;
//                }
//                musicPlayView.indicatorView.setLayoutParams(params);
//                //显示进度
//                musicPlayView.indicatorView.setVisibility(View.VISIBLE);
//                musicPlayView.seekBar.setVisibility(View.VISIBLE);
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                serviceBinder.seekTo(seekBar.getProgress());
//            }
//        });
//
//        musicPlayView.seekBar_high_frequency.setOnSeekBarChangeListener(new SeekBar
//                .OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar
//                    , int progress, boolean fromUser) {
//                // 设置人声高音的强度
//                serviceBinder.UpdateSetEqualizer(progress, true);
//                editorn = mc.edit();
//                editorn.putInt("HighProgress", progress);
//                editorn.commit();
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//            }
//        });
//
        musicPlayView.seekBarBass.setOnSeekBarChangeListener(new SeekBar
                .OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar
                    , int progress, boolean fromUser) {
                // 设置重低音的强度
                serviceBinder.setStrength((short) progress);
                // 设置普通低音的强度
                serviceBinder.UpdateSetEqualizer(progress, false);
                editorn = mc.edit();
                editorn.putInt("BassProgress", progress);
                editorn.commit();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
//        musicPlayView.btnTtypeSingle.setOnClickListener(this);
//        musicPlayView.btnTtypeOrder.setOnClickListener(this);
//        musicPlayView.btnTtypeRandom.setOnClickListener(this);
//        musicPlayView.btnFav.setOnClickListener(this);
//        musicPlayView.btnPlayMore.setOnClickListener(this);
//        musicPlayView.closeView.setOnClickListener(this);
//
//        musicPlayView.fav.setOnClickListener(this);
//         绑定service
//        Intent i = new Intent(this, MusicService.class);
//        bindService(i, mServiceConnection, BIND_AUTO_CREATE);
//
//        Intent intent = new Intent(this, MusicService.class);
//        startService(intent);
//        bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
//
//        this.registerVolumeChangedReceiver();
    }

    public void showMoreWindow(Context context) {

        if (musicPlayView.moreWindow == null) {
            musicPlayView.moreWindow = new PopupWindow(musicPlayView);
            musicPlayView.moreWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            musicPlayView.moreWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            musicPlayView.moreWindow.setContentView(musicPlayView.moreWindowContentView);
            musicPlayView.moreWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
            musicPlayView.moreWindow.setOutsideTouchable(true);
            musicPlayView.moreWindow.setFocusable(true);
            musicPlayView.moreWindow.setAnimationStyle(R.style.popwindow_anim_style);
            musicPlayView.moreWindow.setOnDismissListener(() -> {
                WindowManager.LayoutParams lp1 = ((AppCompatActivity) context).getWindow().getAttributes();
                lp1.alpha = 1f;
                ((AppCompatActivity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                ((AppCompatActivity) context).getWindow().setAttributes(lp1);
            });
        }
        if (musicPlayView.moreWindow.isShowing()) {
            musicPlayView.moreWindow.dismiss();
        } else {
            WindowManager.LayoutParams lp = ((AppCompatActivity) context).getWindow().getAttributes();
            lp.alpha = 0.3f; //设置透明度
            ((AppCompatActivity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            ((AppCompatActivity) context).getWindow().setAttributes(lp);
            musicPlayView.moreWindow.showAtLocation(musicPlayView, Gravity.BOTTOM, 0, 0);
        }

        AudioManager am = (AudioManager) ((AppCompatActivity) context).getSystemService(Context.AUDIO_SERVICE);
        //获取系统最大音量
        int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        musicPlayView.seekBarVolume.setMax(maxVolume);

        musicPlayView.seekBarBass.setMax(1000);
        musicPlayView.seekBar_high_frequency.setMax(1000);

        //获取当前音量
        int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        musicPlayView.seekBarVolume.setProgress(currentVolume);
        musicPlayView.seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    //设置系统音量
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
                    int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
                    seekBar.setProgress(currentVolume);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        //获得当前低音
//        musicPlayView.seekBarBass.setProgress(serviceBinder.getStrength());
        //设置当前
        musicPlayView.seekBarBass.setProgress(mc.getInt("BassProgress", 500));
        musicPlayView.seekBar_high_frequency.setProgress(mc.getInt("HighProgress", 500));

    }

//    //定义与服务的连接的匿名类
//    private ServiceConnection mServiceConnection = new ServiceConnection() {
//
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder service) {
//            //绑定成功后，取得MusicSercice提供的接口
//            serviceBinder = (MusicService.MusicServiceBinder) service;
//
//            //注册监听器
//            serviceBinder.registerOnStateChangeListener(listenr);
//
//            //获得当前音乐
//            Music item = serviceBinder.getCurrentMusic();
//
//            musicPlayView.seekBar.setEnabled(true);
//            if (item == null) {
//                //当前音乐为空, seekbar不可拖动
//                musicPlayView.seekBar.setEnabled(false);
//            } else if (serviceBinder.isPlaying()) {
//                //如果正在播放音乐, 更新信息
//                musicPlayView.musicTitleView.setText(item.musicName);
//                String logoUrl = MusicFavManager.sharedManager().getLogoUrl(item.cateId);
//                if (logoUrl != null && !logoUrl.isEmpty()) {
//                    Glide.with(BaseApplication.getInstance()).load(logoUrl).error(R.mipmap.headimg).into(musicPlayView.musicImgView);
//                }
//                musicPlayView.btnPlayOrPause.setImageResource(R.mipmap.ic_music_pause);
//
//            } else {
//                //当前有可播放音乐但没有播放
//                musicPlayView.musicTitleView.setText(item.musicName);
//                String logoUrl = MusicFavManager.sharedManager().getLogoUrl(item.cateId);
//                if (logoUrl != null && !logoUrl.isEmpty()) {
//                    Glide.with(BaseApplication.getInstance()).load(logoUrl).error(R.mipmap.headimg).into(musicPlayView.musicImgView);
//                }
//                musicPlayView.btnPlayOrPause.setImageResource(R.mipmap.ic_music_play);
//
//            }
//            // 获取当前播放模式
//            int mode = (serviceBinder.getPlayMode());
//            musicPlayView.updateMusicType(mode);
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName name) {
//            //断开连接之后, 注销监听器
//            serviceBinder.unregisterOnStateChangeListener(listenr);
//        }
//    };

//    //实现监听器监听MusicService的变化，
//    private MusicService.OnStateChangeListenr listenr = new MusicService.OnStateChangeListenr() {
//
//        @Override
//        public void onPlayProgressChange(long played, long duration) {
//            musicPlayView.seekBar.setMax((int) duration);
//
//            musicPlayView.seekBar.setProgress((int) played);
//        }
//
//        @Override
//        public void onPlay(final Music item) {
//            //变为播放状态时
//            musicPlayView.seekBar.setEnabled(true);
//            musicPlayView.musicTitleView.setText(item.musicName);
//            String logoUrl = MusicFavManager.sharedManager().getLogoUrl(item.cateId);
//            if (logoUrl != null && !logoUrl.isEmpty()) {
//                Glide.with(BaseApplication.getInstance()).load(logoUrl).error(R.mipmap.headimg).into(musicPlayView.musicImgView);
//            }
//            musicPlayView.btnPlayOrPause.setImageResource(R.mipmap.ic_music_pause);
//        }
//
//        @Override
//        public void onPause() {
//            //变为暂停状态时
//            musicPlayView.btnPlayOrPause.setImageResource(R.mipmap.ic_music_play);
//        }
//    };


    public void showBluetoothList() {
        startActivity(new Intent(MainMusicBedActivity.this, BlueToothListActivity.class));
    }


//    //音乐计时器
//    private void showTimerDialog() {
//
//        long limit = serviceBinder.getMusicAlarmLimit();
//        String limitText = "定时关闭";
//        if (limit > 0) {
//            limitText = secondToTime(limit / 1000);
//            startTimer();
//        }
//
//        List hours = new ArrayList();
//        List minutes = new ArrayList();
//        for (int i = 0; i < 24; i++) {
//            hours.add(i + " 小时");
//        }
//        for (int i = 1; i < 60; i++) {
//            minutes.add(i + " 分钟");
//        }
//        pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int options1, int option2, int options3, View v) {
//                //计算秒 注意分钟从1开始
//                stopSecond = options1 * 60 * 60 + (option2 + 1) * 60;
//                serviceBinder.stopMusicAlarm(stopSecond);
//            }
//        }).setTitleText(limitText)
//                .setSubmitText("开始定时")
//                .setCancelText("关闭定时")
//                .addOnCancelClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        serviceBinder.cancelMusicAlarm();
//                        stopTimer();
//                    }
//                })
//                .build();
//        pvOptions.setNPicker(hours, minutes, null);
//        pvOptions.show();
//    }


//    @Override
//    public void addFavorite(int musicId) {
//
//        //获得当前音乐
//        Music item = serviceBinder.getCurrentMusic();
//        Toast.makeText(this, "已将“" + item.musicName + "”添加到【我的收藏】", Toast.LENGTH_SHORT).show();
//
//        MusicFavManager.sharedManager().addFavorite(item);
//        //更新列表
//        EventBus.getDefault().post(new MusicChangeEvent(musicId));
//
//    }
//
//    @Override
//    public void removeFavorite(int musicId) {
//        //获得当前音乐
//        Music item = serviceBinder.getCurrentMusic();
//        Toast.makeText(this, "已将“" + item.musicName + "”从【我的收藏】中删除", Toast.LENGTH_SHORT).show();
//
//        MusicFavManager.sharedManager().removeFavorite(musicId);
//        //更新列表
//        EventBus.getDefault().post(new MusicChangeEvent(musicId));
//
////        //正在播放的
////        Music currentMusic = serviceBinder.getCurrentMusic();
////        //是否正在播放
////        if (currentMusic != null && musicId == currentMusic.id) {
////            serviceBinder.playNext();
////        }
////        serviceBinder.removeMusicById(musicId);
//    }
//
//    // 切换音乐消息
//    @Subscribe
//    public void onMessageEvent(MusicChangeEvent event) {
//        //获得当前音乐
//        Music item = serviceBinder.getCurrentMusic();
//
//        //得到收藏状态
//        if (item != null) {
//            if (MusicFavManager.sharedManager().isExist(item.id)) {
//                musicPlayView.updateFav(true);
//            } else {
//                musicPlayView.updateFav(false);
//            }
//        }
//    }

    /**
     * 返回时分秒
     *
     * @param second
     * @return
     */
    private String secondToTime(long second) {
        long hours = second / 3600;//转换小时数
        second = second % 3600;//剩余秒数
        long minutes = second / 60;//转换分钟
        second = second % 60;//剩余秒数
        return hours + ":" + minutes + ":" + second + " 后关闭音乐";
    }


//    //开始倒计时
//    private void startBatterylevelTimer() {
//        if (batterylevelTimer == null) {
//            batterylevelTimer = new Timer(true);
//        }
//        batterylevelTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                getBluetoothDeviceBattery();
//            }
//        }, 0, 1000);
//    }
//
//    private void stopBatterylevelTimer() {
//        if (batterylevelTimer != null) {
//            batterylevelTimer.cancel();
//            batterylevelTimer = null;
//        }
//    }

//    //开始倒计时
//    private void startTimer() {
//        if (mTimer == null) {
//            mTimer = new Timer(true);
//        }
//        mTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                long limit = serviceBinder.getMusicAlarmLimit();
//                if (0 == limit / 1000) {
//                    stopTimer();
//                    MainMusicBedActivity.this.runOnUiThread(new Runnable() {
//                        public void run() {
//                            pvOptions.setTitleText("定时关闭");
//                        }
//                    });
//                    return;
//                }
//
//                MainMusicBedActivity.this.runOnUiThread(new Runnable() {
//                    public void run() {
//                        long limit = serviceBinder.getMusicAlarmLimit();
//                        String limitText = "定时关闭";
//                        if (limit > 0) {
//                            limitText = secondToTime(limit / 1000);
//                        }
//                        pvOptions.setTitleText(limitText);
//                    }
//
//                });
//            }
//        }, 0, 1000);
//    }
//
//    private void stopTimer() {
//        if (mTimer != null) {
//            mTimer.cancel();
//            mTimer = null;
//        }
//    }

//    /**
//     * 注册当音量发生变化时接收的广播
//     */
//    private void registerVolumeChangedReceiver() {
//        mVolumeReceiver = new MyVolumeReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("android.media.VOLUME_CHANGED_ACTION");
//        registerReceiver(mVolumeReceiver, filter);
//    }
//
//    private void unregisterVolumeChangedReceiver() {
//        if (mVolumeReceiver != null) {
//            unregisterReceiver(mVolumeReceiver);
//        }
//    }

//    /**
//     * 处理音量变化时的界面显示
//     */
//    private class MyVolumeReceiver extends BroadcastReceiver {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            //如果音量发生变化则更改seekbar的位置
//            if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
//
//                AudioManager am = (AudioManager) ((AppCompatActivity) context).getSystemService(Context.AUDIO_SERVICE);
//                //获取当前音量
//                int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
////                Log.i("currentVolume：",currentVolume+"");
//                musicPlayView.seekBarVolume.setProgress(currentVolume);
//            }
//        }
//    }

    /**
     * description: 控制播放器显示
     *
     * @author visitant
     * @time 2021/4/7 14:58
     */
    public void showMusicPlayView(int position) {
        if (position % 3 == 1) {
            musicPlayView.setVisibility(View.GONE);
        } else {
            musicPlayView.setVisibility(View.VISIBLE);
        }
    }

}



